package ${package.ServiceImpl};

import ${package.Entity}.${entity};
import ${util.getVoPackage(package.Entity)}.${entity}Vo;
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.house.framework.annotations.Slave;
<#if (util.getPrimaryKeys(table.fields)?size ==1)>
import com.house.common.IdBuilder;
</#if>
/**
 * <p>
 * ${table.comment!} 服务实现类
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {

    @Autowired
    private ${table.mapperName} ${util.lowerFirst(table.mapperName)};

    /**
    * 新增${table.comment}
    * @param entity
    * ${table.name}新增数据
    * @return
    */
    @SneakyThrows
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    <#if (util.getPrimaryKeys(table.fields)?size ==1)>
    <#assign primaryField=util.getPrimaryKeys(table.fields)[0]/>
    <#assign entityPrimaryFieldName=util.getUpperFieldName(primaryField.name)/>
    public ${primaryField.propertyType} add${entity}(${entity} entity){
        <#if (primaryField.propertyType =="String")>
        String id = IdBuilder.getUUID();
        <#else>
        long id = IdBuilder.getSnowId();
        </#if>
        entity.set${entityPrimaryFieldName}(id);

        ${util.lowerFirst(table.mapperName)}.insert(entity);
        return id;
    }
    <#else>
    public void add${entity}(${entity} entity){
        ${util.lowerFirst(table.mapperName)}.insert(entity);
    }
    </#if>

    /**
    * 修改${table.comment}
    * @param entity
    * ${table.name}修改数据
    * @return
    */
    @SneakyThrows
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int update${entity}(${entity} entity){
        return ${util.lowerFirst(table.mapperName)}.updateById(entity);
    }
    <#if (util.getPrimaryKeys(table.fields)?size ==1)>

    /**
    * 取指定ID的${table.comment}数据
    * @param id
    * ${table.name} 主键ID
    * @return ${entity}Vo
    * 返回指定ID的${table.comment}数据
    */
    <#assign primaryField=util.getPrimaryKeys(table.fields)[0]/>
    @Slave
    @Override
    public ${entity}Vo get${entity}ById(${primaryField.propertyType} id) {
        return ${util.lowerFirst(table.mapperName)}.get(id);
    }
    </#if>

    /**
    * ${table.comment}分页取数
    * @param entityVo
    * ${table.name} 分页取数参数，pageIndex=页码；pageSize=每页记录数；
    * @return
    * 返回分页数据
    */
    @Slave
    @Override
    public IPage<${entity}Vo> get${entity}List(${entity}Vo entityVo) {
        int pageIndex = entityVo.getPageIndex();
        int pageSize = entityVo.getPageSize();
        IPage page = new Page<${entity}Vo>(pageIndex, pageSize);
        QueryWrapper<${entity}Vo> queryWrapper = new QueryWrapper<>();
        //取数条件
        //queryWrapper.eq("字段名",字段值);
        //……
        //排序字段
        //queryWrapper.orderByAsc("正序字段");
        //请修改此排序字段,为了让生成的代码分页不会报错，所以随意放了一个字段
        <#if (util.getPrimaryKeys(table.fields)?size ==1)>
        <#assign primaryField=util.getPrimaryKeys(table.fields)[0]/>
        queryWrapper.orderByDesc("${primaryField.propertyName}");
        <#else>
        queryWrapper.orderByDesc("${table.fields[0].propertyName}");
        </#if>
        return ${util.lowerFirst(table.mapperName)}.page(page, queryWrapper);
    }
    <#if (util.getPrimaryKeys(table.fields)?size ==1)>

    /**
    * 删除指定ID的${table.comment}数据
    * @param id
    * ${table.name} 主键ID
    */
    <#assign primaryField=util.getPrimaryKeys(table.fields)[0]/>
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void delete${entity}ById(${primaryField.propertyType} id){
        ${util.lowerFirst(table.mapperName)}.delete(id);
    }

    /**
    * 逻辑删除指定ID的${table.comment}数据
    * @param id
    * ${table.name} 主键ID
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void logicDeleteById(${primaryField.propertyType} id){
        this.logicDelete(id, ${table.mapperName}.class, ${entity}.class);
    }
    </#if>

    <#if (util.getPrimaryKeys(table.fields)?size ==1)>

    /**
    * 批量删除指定ID的${table.comment}数据
    * @param ids
    * ${table.name} 主键ID数组
    */
    <#assign primaryField=util.getPrimaryKeys(table.fields)[0]/>
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void batchDelete(${primaryField.propertyType}[] ids){
        ${util.lowerFirst(table.mapperName)}.batchDelete(ids);
    }

    /**
    * 批量逻辑删除指定ID的${table.comment}数据
    * @param ids
    * ${table.name} 主键ID数组
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void batchLogicDelete(${primaryField.propertyType}[] ids){
        for(${primaryField.propertyType} id : ids){
            this.logicDelete(id, ${table.mapperName}.class, ${entity}.class);
        }
    }
    </#if>
}
</#if>
